import requests
from bs4 import BeautifulSoup as bs
from pyquery import PyQuery as pq
import json
from sql_helper import MySqlHelper


def read_list(url, item_dict_all=None):
    if item_dict_all is None:
        item_dict_all = []
    response = requests.get(url)
    if response.status_code == 200:
        box = pq(url=url)
        s_box = box('#content')
        li_list = s_box.find('ul').children('li').items()

        for item in li_list:
            item_dict = []
            img = item.find('img').attr.src
            print(img)
            box = item.find('div')
            title = box.find('h2').text()
            print(title)
            message = box.find('.color-gray').text().split('/')
            author = message[0]
            print(author)
            source = message[-2].strip()
            print(source)
            pub_time = message[-1].strip()
            print(pub_time)
            intro = item.find('div > p:nth-child(4)').text().strip()
            print(intro)
            a = box('a').attr.href
            print(a)
            item_dict.append(img)
            item_dict.append(title)
            item_dict.append(author)
            item_dict.append(source)
            item_dict.append(intro)
            item_dict.append(a)
            item_dict_all.append(item_dict)
        print(item_dict_all)
    else:
        print("你抓的出问题了！")
    return item_dict_all



if __name__ == "__main__":
    url = "https://book.douban.com/latest?icn=index-latestbook-all"
    item_dict_all = read_list(url)
    mysql = MySqlHelper()
    sql = "INSERT INTO books(img, title, author, source, intro, a) VALUES(%s,%s,%s,%s,%s,%s)"
    mysql.exec_many(sql, item_dict_all)
